#define DEBUG
#include <cstdio>
#include <cstring>
#define MAXN 1000000
#define INF 0x7ffffff

using namespace std;

void sort(int *a, int l, int r) {
  int i=l, j=r, mid=a[(l+r)/2], temp;
  for (; i<=j; ) {
    for (; a[j]>mid; j--);
    for (; a[i]<mid; i++);
    if (i<=j) {
      temp=a[i], a[i]=a[j], a[j]=temp;
      i++, j--;
    } 
  }
  if (i<r) {
    sort(a, i, r);
  }
  if (j>l) {
    sort(a, l, j);
  }
}

int min(int a, int b) {
  return a<b ? a : b;
}

void push(int q[][2], int v, int s) {
  if (q[0][0]>q[0][1]) {
    q[++q[0][1]][0] = v;
  } else {
    for (; q[0][0]<=q[0][1] && q[q[0][1]][0]>=v; q[0][1]--);
    q[++q[0][1]][0] = v;
  }
  q[q[0][1]][1] = s;
}

void pop(int q[][2], int s) {
  for (; q[0][0]<=q[0][1] && q[q[0][0]][1]<=s; q[0][0]++);
}

int main() {
#ifdef DEBUG
  freopen("0.in", "r", stdin);
  freopen("0.out", "w", stdout);
#endif

  static int v[MAXN+1];
  int n, k;
  scanf("%d %d", &n, &k);
  for (int i=1; i<=n; i++) {
    scanf("%d", v+i);
  }

  sort(v, 1, n);

  static int que[MAXN+1][2];
  que[0][0]=1, que[0][1] = 0;
  for (int i=2; i<=n-k-1; i++) {
    push(que, v[i]-v[i-1], i-1);
  }

  int ans=INF;
  for (int i=n-k; i<=n; i++) {
    push(que, v[i]-v[i-1], i-1);
    pop(que, i-n+k);
    ans = min(ans, v[i]-v[i-n+k+1]+que[que[0][0]][0]);
  }
  printf("%d", ans);

  fcloseall();
  return 0;
}
